﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Media;

namespace vmlModel.Entities
{
    public enum FlipDir { X, Y, XY, None }
   public  class vmlShapeBase : vmlObject
    {
        #region "Style Atrrs"

        public string vmlObjStyle { get { return (vmlObjElement.Attribute("style") != null) ? vmlObjElement.Attribute("style").Value : ""; } }
        public string position { get { return (vmlObjElement.Attribute("position") != null) ? vmlObjElement.Attribute("position").Value : ""; } }
        public int Zindex { get { return (vmlObjElement.Attribute("z-index") != null) ? int.Parse(vmlObjElement.Attribute("z-index").Value) : 0; } }
        public string TextAlign { get { return (vmlObjElement.Attribute("text-align") != null) ? vmlObjElement.Attribute("text-align").Value : ""; } }
        public double Left
        {
            get
            {
                if (vmlObjElement.Attribute("left") != null)
                {
                    string atrrValue = vmlObjElement.Attribute("left").Value;
                    return vmlUtils.GetLength(atrrValue);
                }
                return 0;
            }
        }
        public double Top
        {
            get
            {
                if (vmlObjElement.Attribute("top") != null)
                {
                    string atrrValue = vmlObjElement.Attribute("top").Value;
                    return vmlUtils.GetLength(atrrValue);
                }
                return 0;
            }
        }
        public double Width
        {
            get
            {
                if (vmlObjElement.Attribute("width") != null)
                {
                    string atrrValue = vmlObjElement.Attribute("width").Value;
                    return vmlUtils.GetLength(atrrValue);
                }
                return 0;
            }
        }
        public double Height
        {
            get
            {
                if (vmlObjElement.Attribute("height") != null)
                {
                    string atrrValue = vmlObjElement.Attribute("height").Value;
                    return vmlUtils.GetLength(atrrValue);
                }
                return 0;
            }
        }
        public double MarginLeft
        {
            get
            {
                if (vmlObjElement.Attribute("margin-left") != null)
                {
                    string atrrValue = vmlObjElement.Attribute("margin-left").Value;
                    return vmlUtils.GetLength(atrrValue);
                }
                return 0;
            }
        }
        public double MarginTop
        {
            get
            {
                if (vmlObjElement.Attribute("margin-top") != null)
                {
                    string atrrValue = vmlObjElement.Attribute("margin-top").Value;
                    return vmlUtils.GetLength(atrrValue);
                }
                return 0;
            }
        }
        public FlipDir Flip
        {
            get
            {
                if (vmlObjElement.Attribute("flip") != null)
                {
                    string atrrValue = vmlObjElement.Attribute("flip").Value;
                    switch (atrrValue.ToLower())
                    {
                        case "x":
                            return FlipDir.X;
                        case "y":
                            return FlipDir.Y;
                        case "x y":
                            return FlipDir.XY;

                        default:
                            return FlipDir.None;
                    }
                }
                return FlipDir.None ;
            }
        }

        #endregion

        #region "appearance Atrrs"
        public Color FillColor { get { return (vmlObjElement.Attribute("fillcolor") != null) ? vmlUtils.StringToColor(vmlObjElement.Attribute("fillcolor").Value.Split(char.Parse(" "))[0]) : Colors.Transparent; } }
        public Color StrokeColor { get { return (vmlObjElement.Attribute("strokecolor") != null) ? vmlUtils.StringToColor(vmlObjElement.Attribute("strokecolor").Value.Split(char.Parse(" "))[0]) : Colors.Black; } }
        public double StrokeWeight
        {
            get
            {
                if (vmlObjElement.Attribute("strokeweight") != null)
                {
                    string atrrValue = vmlObjElement.Attribute("strokeweight").Value;
                    return vmlUtils.GetLength(atrrValue);
                }
                return 1;
            }
        }
        #endregion

        #region "SubElements"
        public vmlStroke Stroke {get; set;}
        public vmlFill Fill { get; set; }
        public vmlShadow Shadow { get; set; }
        #endregion

        public vmlShapeBase()
        { 
        }
        public vmlShapeBase(XElement _vmlObjElement)
            : base(_vmlObjElement)
        {
           //----------- Style -----------------------------------------------------------------------
           XAttribute _objStyle = vmlObjElement.Attribute("style");
           if (_objStyle != null)
           {
               foreach (string property in _objStyle.Value.Split(';'))
               {
                   string[] members = property.Split(':');
                   if (members.Length == 2)
                       try
                       {
                           vmlObjElement.SetAttributeValue(members[0], members[1]);
                       }
                       catch 
                       {
                           continue;
                       }
               }
           }

           FillMe();

         }

        private void FillMe()
        {
            // get the SubElements
            foreach (XElement element in from elemnt in vmlObjElement.Elements()
                                         select elemnt)
            {
                switch (element.Name.LocalName.ToLower())
                {
                    case "stroke":
                        Stroke = new vmlStroke(element);
                        break;
                    case "fill":
                        Fill = new vmlFill(element);
                        break;
                    case "shadow":
                        Shadow  = new vmlShadow(element);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
